package com.inei.model.connection;

import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.inei.R;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class UtilDB extends SQLiteOpenHelper {

	protected static Context context;
	public static String DATABASE = "cengrel.db";
	public static int VERSION = 1;

	public UtilDB(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public UtilDB(Context ctx) {
		super(ctx, DATABASE, null, VERSION);
		context = ctx;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql;
		InputStream in;
		Document doc;
		NodeList statements;
		int i;
		try {
			DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			// MarcoCenso
			in = context.getResources().openRawResource(R.raw.sqlmarcocenso);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("MarcoCenso");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// RespuestaCenso
			in = context.getResources()
					.openRawResource(R.raw.sqlrespuestacenso);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("RespuestaCenso");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// RespuestaCensoWeb
			in = context.getResources().openRawResource(
					R.raw.sqlrespuestacensoweb);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("RespuestaCensoWeb");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}
		} catch (Exception e) {

		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
